﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Single.Utils
{
    public static class DateTimeHelper
    {
        public static DateTime FromUnixTimeSeconds(long timestamp)
        {
            TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
            DateTimeOffset offset = DateTimeOffset.FromUnixTimeSeconds(timestamp);
            return TimeZoneInfo.ConvertTimeFromUtc(offset.UtcDateTime, timeZone);
        }

        public static DateTime FromUnixTimeMilliseconds(long timestamp)
        {
            TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
            DateTimeOffset offset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
            return TimeZoneInfo.ConvertTimeFromUtc(offset.UtcDateTime, timeZone);
        }

        public static DateTime FromUnixTimeSeconds(string timestamp)
        {
            return long.TryParse(timestamp, out long result) ? FromUnixTimeSeconds(result) : DateTime.MinValue;
        }

        public static DateTime FromUnixTimeMilliseconds(string timestamp)
        {
            return long.TryParse(timestamp, out long result) ? FromUnixTimeMilliseconds(result) : DateTime.MinValue;
        }

        public static long ToUnixTimeSeconds(DateTime dateTime)
        {
            TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
            DateTime utcDateTime = dateTime.ToUniversalTime();
            DateTimeOffset offset = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZone);
            return offset.ToUnixTimeSeconds();
        }

        public static long FromUnixTimeMilliseconds(DateTime dateTime)
        {
            TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
            DateTime utcDateTime = dateTime.ToUniversalTime();
            DateTimeOffset offset = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZone);
            return offset.ToUnixTimeMilliseconds();
        }
    }
}